import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class Board extends JPanel implements MouseListener, ActionListener{
	
	private int[][] grid = new int[3][3];
	
	private Square a11 = new Square(20, 80);
	private Square a12 = new Square(140, 80);
	private Square a13 = new Square(260, 80);
	
	private Square a21 = new Square(20, 200);
	private Square a22 = new Square(140, 200);
	private Square a23 = new Square(260, 200);
	
	private Square a31 = new Square(20, 320);
	private Square a32 = new Square(140, 320);
	private Square a33 = new Square(260, 320);

	private ImageIcon ttt = new ImageIcon(getClass().getResource("ttt.png"));
	private ImageIcon xwin = new ImageIcon(getClass().getResource("xwin.png"));
	private ImageIcon owin = new ImageIcon(getClass().getResource("owin.png"));
	private ImageIcon draw = new ImageIcon(getClass().getResource("draw.png"));
	private Image chalkBoard;
	private Image message;
	
	private boolean run = true;
	private int count = 0;
	
	private Timer pause = new Timer(3000, this);

	public Board () {

		chalkBoard = ttt.getImage();
	
		for (int i = 0; i < 3; ++i)
			for (int j = 0; j < 3; ++j)
				grid[i][j] = 0;
				
		addMouseListener(this);

	}

	public void paintComponent(Graphics g) {
		g.setColor(Color.green);
		g.drawImage(chalkBoard, 0, 0, this);
		a11.paint(g);
		a12.paint(g);
		a13.paint(g);
		a21.paint(g);
		a22.paint(g);
		a23.paint(g);
		a31.paint(g);
		a32.paint(g);
		a33.paint(g);	
		if (run == false)
			g.drawImage(message, 20, 180, this);	
	}
	
	public void setGrid(int x, int y, int player) {
		--x;
		--y;
		grid[x][y] = player;
	}
	
	public void checkGrid() {
		int queue1, queue2;
		
		for (int i = 0; i < 3; ++i) {
			queue1 = grid[i][0];
			queue2 = grid[i][1];
			
			if (grid[i][2] == queue1 && grid[i][2] == queue2 && queue1 != 0) {	
				pause.start();
				if (queue1 == 1)
					message = xwin.getImage();
				else
					message = owin.getImage();
				run = false;
			}
			queue1 = queue2 = 0;
		
		}
		
		for (int i = 0; i < 3; ++i) {
			queue1 = grid[0][i];
			queue2 = grid[1][i];
			if (grid[2][i] == queue1 && grid[2][i] == queue2 && queue1 != 0) {
				pause.start();
				if (queue1 == 1)
					message = xwin.getImage();
				else
					message = owin.getImage();
				run = false;
			}
			queue1 = queue2 = 0;
		}
		
		if (grid[0][0] == grid[1][1] && grid[0][0] == grid[2][2] && grid[0][0] != 0) {
			pause.start();
			if (grid[1][1] == 1)
				message = xwin.getImage();
			else
				message = owin.getImage();
			run = false;
		}
		
		if (grid[2][0] == grid[1][1] && grid[2][0] == grid[0][2] && grid[2][0] != 0) {
			pause.start();
			if (grid[1][1] == 1)
				message = xwin.getImage();
			else
				message = owin.getImage();
			run = false;
		}

		if (count == 9 && run == true) {
			pause.start();
			message = draw.getImage();
			run = false;
		}
	}
	
	public void reset () {
	
		for (int i = 0; i < 3; ++i)
			for (int j = 0; j < 3; ++j)
				grid[i][j] = 0;
						
		count = 0;
				
		a11.reset();
		a12.reset();
		a13.reset();

		a21.reset();
		a22.reset();
		a23.reset();
		
		a31.reset();
		a32.reset();
		a33.reset();
		repaint();
		run = true;
	}
		
	public void actionPerformed(ActionEvent ev) {
		reset();
		pause.stop();
	}
	
	public void mousePressed(MouseEvent e) {
		
		if (e.getX() >= 20 && e.getX() <= 120 && e.getY() >= 80 && e.getY() <=180 && run == true) {
			if (a11.Change(count)) {
				if (a11.player())
					setGrid(1, 1, 1);
				else
					setGrid(1, 1, 2);
				repaint();
				++count;
			}
		}
		
		if (e.getX() >= 140 && e.getX() <= 240 && e.getY() >= 80 && e.getY() <=180 && run == true) {
			if (a12.Change(count)) {
				if (a12.player())
					setGrid(1, 2, 1);
				else
					setGrid(1, 2, 2);
				repaint();
				++count;
			}
		}
		
		if (e.getX() >= 260 && e.getX() <= 360 && e.getY() >= 80 && e.getY() <=180 && run == true) {
			if (a13.Change(count)) {
				if (a13.player())
					setGrid(1, 3, 1);
				else
					setGrid(1, 3, 2);
				repaint();
				++count;
			}
		}
		
		if (e.getX() >= 20 && e.getX() <= 120 && e.getY() >= 200 && e.getY() <=300 && run == true) {
			if (a21.Change(count)) {
				if (a21.player())
					setGrid(2, 1, 1);
				else
					setGrid(2, 1, 2);
				repaint();
				++count;
			}
		}
		
		if (e.getX() >= 140 && e.getX() <= 240 && e.getY() >= 200 && e.getY() <=300 && run == true) {
			if (a22.Change(count)) {
				if (a22.player())
					setGrid(2, 2, 1);
				else
					setGrid(2, 2, 2);
				repaint();
				++count;
			}
		}
		
		if (e.getX() >= 260 && e.getX() <= 360 && e.getY() >= 200 && e.getY() <= 300 && run == true) {
			if (a23.Change(count)) {
				if (a23.player())
					setGrid(2, 3, 1);
				else
					setGrid(2, 3, 2);
				repaint();
				++count;
			}
		}
		
		if (e.getX() >= 20 && e.getX() <= 120 && e.getY() >= 320 && e.getY() <= 420 && run == true) {
			if (a31.Change(count)) {
				if (a31.player())
					setGrid(3, 1, 1);
				else
					setGrid(3, 1, 2);
				repaint();
				++count;
			}
		}
		
		if (e.getX() >= 140 && e.getX() <= 240 && e.getY() >= 320 && e.getY() <= 420 && run == true) {
			if (a32.Change(count)) {
				if (a32.player())
					setGrid(3, 2, 1);
				else
					setGrid(3, 2, 2);
				repaint();
				++count;
			}
		}
		
		if (e.getX() >= 260 && e.getX() <= 360 && e.getY() >= 320 && e.getY() <= 420 && run == true) {
			if (a33.Change(count)) {
				if (a33.player())
					setGrid(3, 3, 1);
				else
					setGrid(3, 3, 2);
				repaint();
				++count;
			}
		}
		
		checkGrid();
	}
	public void mouseReleased(MouseEvent e) {}
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}

}
